package com.cn.yang.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.UserModel;
import com.cn.yang.service.UserService;
import com.cn.yang.service.impl.UserServiceImpl;
import com.cn.yang.util.TableTimeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/user/regester")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1,处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        resp.setHeader("Content-type","text/html;charset=utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        //2，得到页面传过来的值，
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        String introduce = req.getParameter("introduce");
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String createtime = sdf.format(d);
//        3，将值封装到实体类
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setPhone(phone);
        userModel.setSex(sex);
        userModel.setBirthday(birthday);
        userModel.setHobby(hobby);
        userModel.setIntroduce(introduce);
        userModel.setCreatetime(createtime);
        userModel.setUpdatetime(createtime);
//        4，服务逻辑层实例化service
        UserService userService = new UserServiceImpl();
        JSONObject jsonObject = new JSONObject();

        int result = userService.registerUser(userModel);
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);

    }


}
